<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of title_studies_controller
 *
 * @author wilson
 */
class TitleStudiesController extends AppController {

    //put your code here
    var $name = "TitleStudies";

    function index() {
        $id = $this->Session->read('proy_id');
        $this->layout = "ajax";
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } else {
            $this->TitleStudy->Property->recursive = -1;
            $this->set("properties", $this->TitleStudy->Property->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('Property.id', 'Property.codigo', 'Property.nombre', 'Property.vereda', 'Property.area_negociacion', 'Property.area_titulo'))));
            $this->set('cod', $this->Session->read('cod'));
        }
    }

    function title_index($predio) {

        $this->TitleStudy->Property->recursive = -1;
        $nombre = $this->TitleStudy->Property->find('first', array('conditions' => array('Property.id' => $predio), 'fields' => array('Property.nombre', 'Property.codigo')));
        $this->set('cod', $nombre['Property']['codigo'] . " " . $nombre['Property']['nombre']);
        $this->set('predio', $predio);
        $this->TitleStudy->recursive = -1;
        $this->set('studies', $this->TitleStudy->find('all', array('conditions' => array('property_id' => $predio), 'fields' => array('TitleStudy.id', 'TitleStudy.concepto', 'TitleStudy.recomendaciones', 'TitleStudy.calificacion'))));
    }

    function add($predio) {
        $this->set('est', $this->Session->read('estado'));
        $this->set('cod', $predio);
        App::Import('model', 'Requirement');

        $req = new Requirement();
        $req->recursive = -1;
        $this->set('condiciones', $req->find('all', array('conditions' => array('Requirement.requirement_type_id' => 9), 'fields' => array('Requirement.id', 'Requirement.texto'))));
        $this->TitleStudy->Property->recursive = -1;
        $this->set('predio', $this->TitleStudy->Property->find('all', array('conditions' => array('Property.id' => $predio))));
        $this->TitleStudy->Property->Owner->recursive = -1;
        $this->set('owners', $this->TitleStudy->Property->Owner->find('all', array('conditions' => array('Owner.property_id' => $predio))));


        if (empty($this->data)) {
            $this->data['TitleStudy']['property_id'] = $predio;
        } else {
            $datos = $this->params['form'];
            $this->data['TitleStudy']['user_id'] = $this->Auth->user('id');
            $this->TitleStudy->create();
            if ($this->TitleStudy->save($this->data)) {
                $nuevoId = $this->TitleStudy->id;

                foreach ($datos['elegidos'] as $value) {


                    $estudio = array(
                        'requirement_id' => $value,
                        'title_study_id' => $nuevoId,
                        'concepto' => $datos["texto_$value"],
                    );

                    $this->TitleStudy->RequirementsTitleStudy->create();
                    $this->TitleStudy->RequirementsTitleStudy->save($estudio);
                }
                $this->Session->setFlash("Estudio Guardado con exito");
                $this->setAction('title_index', $this->data['TitleStudy']['property_id']);
            }
        }
    }

    function edit($id) {
        $this->set('est', $this->Session->read('estado'));
        App::Import('model', 'Requirement');
        $req = new Requirement();
        if (empty($this->data)) {
            $this->data = $this->TitleStudy->recursive = -1;
            //$this->data = $this->TitleStudy->findById($id);
            $this->data = $this->TitleStudy->find("first", array("conditions" => array("TitleStudy.id" => $id)));
            $req->recursive = -1;
            $this->set('condiciones', $req->find('all', array('conditions' => array('Requirement.requirement_type_id' => 9), 'fields' => array('Requirement.id', 'Requirement.texto'))));
            $this->TitleStudy->RequirementsTitleStudy->recursive = -1;
            $this->set('chequeados', $this->TitleStudy->RequirementsTitleStudy->find('all', array('conditions' => array('RequirementsTitleStudy.title_study_id' => $id), 'fields' => array('RequirementsTitleStudy.requirement_id', 'RequirementsTitleStudy.concepto'))));
            $this->TitleStudy->Property->recursive = -1;
            $this->TitleStudy->Property->Owner->recursive = -1;
            $this->set('predio', $this->TitleStudy->Property->find('all', array('conditions' => array('Property.id' => $this->data['TitleStudy']['property_id']))));
            $this->TitleStudy->Property->Owner->recursive = -1;
            $this->set('owners', $this->TitleStudy->Property->Owner->find('all', array('conditions' => array('Owner.property_id' => $this->data['TitleStudy']['property_id']))));
        } else {
            $datos = $this->params['form'];
            $this->data['TitleStudy']['user_id'] = $this->Auth->user('id');
            if ($this->TitleStudy->save($this->data)) {
                $ide = $this->data['TitleStudy']['id'];
                $this->TitleStudy->RequirementsTitleStudy->query("DELETE FROM requirements_title_studies WHERE title_study_id=$ide");
                if (isset($datos['elegidos']))
                    foreach ($datos['elegidos'] as $value) {


                        $estudio = array(
                            'requirement_id' => $value,
                            'title_study_id' => $ide,
                            'concepto' => $datos["texto_$value"],
                        );

                        $this->TitleStudy->RequirementsTitleStudy->create();
                        $this->TitleStudy->RequirementsTitleStudy->save($estudio);
                    }
                $this->Session->setFlash("Estudio Editado con exito");
                $this->setAction('title_index', $this->data['TitleStudy']['property_id']);
            }
        }
    }

    function delete($id, $predio) {
        //$this->TitleStudy->RequirementsTitleStudy->deleteAll(array('RequirementsTitleStudy.title_study_id'=>$id));
        $this->TitleStudy->create();
        $this->TitleStudy->RequirementsTitleStudy->create();
        $this->TitleStudy->RequirementsTitleStudy->query("DELETE FROM requirements_title_studies WHERE title_study_id=$id");
        $this->TitleStudy->delete($id);
        $this->Session->setFlash("Estudio Borrado con exito");
        $this->setAction('title_index', $predio);
    }

    function edit_correction($id) {
        if (empty($this->data)) {
            $this->data = $this->TitleStudy->find('first', array('conditions' => array('TitleStudy.id' => $id), 'fields' => array('TitleStudy.id', 'TitleStudy.doc_subsanacion', 'TitleStudy.cal_subsanacion', 'TitleStudy.con_subsanacion', 'Property.nombre', 'Property.codigo')));
        } else {



            if ($this->TitleStudy->save($this->data)) {
                $this->Session->setFlash('Registro guardado exitosamente');
                $this->redirect(array('controller' => 'CorrectionLetters', 'action' => 'index'));
            }
        }
    }

    function title_letter() {
        $this->layout = "pdf";
        $id = $this->Session->read('proy_id');
        $codigo = $this->Session->read('cod');
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } else{
            
             $this->set('codigo',$codigo);
            App::Import('model','proyect');
            $pry=new Proyect();
            $pry->Proposer->recursive=0;
            $pry->recursive=0;
            $this->set('pid',$id);
            $this->set('proponente',$pry->Proposer->find('all',array('conditions'=>array('Proposer.proyect_id'=>$id),'fields'=>array('Proposer.nombre','Proposer.direccion','City.name'))));
            $proyecto=$pry->find('first',array('conditions'=>array('Proyect.id'=>$id),'fields'=>array('Branch.nombre','Branch.director','Branch.director','Proyect.resultado_fase1')));
            $this->set('proyecto',$proyecto);
            
            }
    }
}

?>
